package com.lhg.common.listener;

import com.lhg.common.util.RedisUtil;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.Set;

/**
 * 在spring启动完成后，调用此方法
 */
@Component
public class TaskStartSpringListener implements ApplicationListener<ContextRefreshedEvent> {
    private static Logger logger = Logger.getLogger(TaskStartSpringListener.class);

    @Resource
    private RedisUtil redisUtil;

    private int index = 0;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        index++;
        if (index == 2) {//保证只执行一次
            //doSomeThing();
        }
    }

    private void doSomeThing() {
        //清空redis缓存
        Set<String> set = redisUtil.getAllKeys();
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String str = it.next();
            redisUtil.set(str, null);
            System.out.println(str);
        }
    }

}
